define(["jQuery", "kendo"], function ($, kendo) {
    return {
    	panel:null,
    	contentDOMid:null,
    	isShow:false,
		isEditMode:false,
    	creationMode:false,
    	lastTop:0,
    	id:"",
		
		show : function(position){
			$("#nic-sticky-edit-panel-container" ).show();
			App.stickyText.menu.isShow = true;
			App.stickyText.menu.creationMode = true;
		},
		
		hideMenu : function(){
			$("#nic-sticky-edit-panel-container" ).hide();
			App.stickyText.menu.isShow = false;
			App.stickyText.menu.creationMode = false;
			App.stickyText.menu.isEditMode = false;
			$("#"+App.stickyText.menu.id).draggable( 'enable' );	
			$("#"+App.stickyText.menu.id).resizable( 'enable' );	
			App.stickyText.menu.panel.nicInstances.splice(0,1);
			$("#" + App.stickyText.menu.id).attr("contenteditable",false);
			App.prepareCreation = false;
			$("#"+App.stickyText.menu.id).css( 'border','' );
			$("#"+App.stickyText.menu.id).css( 'overflow','visible' );
			App.stickyText.menu.id = "";
			
			//hide any color picker windows if shown.
			$('.jPicker.Container').each(function(){
				if($(this).css('display') == 'block')
				{	
					$(this).css('display','none');
				}
			});
		},
		
		cancel : function(){
			if(!App.stickyText.menu.isEditMode){
				$("#" + App.stickyText.menu.id).remove();
			}else{
				$("#" + App.stickyText.menu.id).css('top',App.stickyText.lastTop);
				$("#text_content_" + App.stickyText.menu.id).html(App.stickyText.textBefore);
				$("#"+App.stickyText.menu.id).css( 'background-color',App.stickyText.colorBefore);
			}
			App.stickyText.menu.hideMenu();
			
		},
		
        init: function (e) {
        	console.log("init panel..");
			
			
			App.stickyText.menu.panel = new nicEditor();
			App.stickyText.menu.panel.setPanel('nic-sticky-edit-panel');
			//myNicEditor.addInstance('nic-edit-element');
			//$("#nic-edit-panel").draggable();
			$("#nic-sticky-edit-panel-container").draggable();
			$('.nicEdit-Picker').hide();
			$('.nicEdit-Picker').jPicker(
					{
					 window:
						  {
							expandable: true
						  },
						  images:
						  {
							clientPath : App.baseUrl+'assets/css/images/jPicker/'
						  }
					},
					function(color, context)
					{
						var all = color.val('all');  
						
						$("#"+App.richText.menu.id).trigger('click'); 
						var colorText = "#"+rgbToHex("rgb("+all.r+","+all.g+","+all.b+")");  
						document.execCommand("foreColor", false, colorText); 
					}
			);
			$('#stickyJPicker').hide();
			$('#stickyJPicker').jPicker(
					{
					 window:
						  {
							expandable: true
						  },
						  images:
						  {
							clientPath : App.baseUrl+'assets/css/images/jPicker/'
						  }
					},
					function(color, context)
					{
						var all = color.val('all');  
						
						// $("#"+App.richText.menu.id).trigger('click'); 
						var colorText = "#"+rgbToHex("rgb("+all.r+","+all.g+","+all.b+")");  
						$("#"+App.stickyText.menu.id).css( 'background-color', colorText);
						// document.execCommand("foreColor", false, colorText); 
					}
			);
			
			var rgbToHex = (function () {
				var rx = /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i;

				function pad(num) {
					if (num.length === 1) {
						num = "0" + num;
					}

					return num;
				}

				return function (rgb, uppercase) {
					var rxArray = rgb.match(rx),
						hex;

					if (rxArray !== null) {
						hex = pad(parseInt(rxArray[1], 10).toString(16)) + pad(parseInt(rxArray[2], 10).toString(16)) + pad(parseInt(rxArray[3], 10).toString(16));

						if (uppercase === true) {
							hex = hex.toUpperCase();
						}

						return hex;
					}

					return;
				};
			}());
			$('.sticky-text-tools').live('click', function(e){
				if(!App.toolboxValidation("tool_text_sticky_ico"))
				{
					return
				}
				$(this).addClass('active');
				App.openedTool = $(this);
				$("#sticky-dragable-menu").offset({top:$(this).offset().top,left:$(this).offset().left});
				App.stickyToolbox.show();
			});	
			
			$("#sticky-text-save").click(function(){
				App.openedTool.removeClass('active');
				App.openedTool =false;
				if(!App.stickyText.menu.isEditMode)
				{
					App.addStickyTextToServer(App.stickyText.menu);
				}
				else
				{
				
					$("#" + App.stickyText.menu.id).css('top',App.stickyText.lastTop);
					var emit = $("#"+App.stickyText.menu.id).clone(); 
					var id = $("#"+App.stickyText.menu.id).attr("id");
					
					var width = $("#"+App.stickyText.menu.id).width();
					var height = $("#"+App.stickyText.menu.id).height();
					
					var top =  $("#"+App.stickyText.menu.id).position().top;
					var left =  $("#"+App.stickyText.menu.id).position().left;
					console.log("top "+top);
					console.log("left "+left);
					var objectJson = {
						"content":$("#text_content_"+id).html(),
						"id": id, 
						"posy": top, 
						"posx": left, 
						"height": parseInt(height), 
						"width": parseInt(width),
						"background":$("#"+id).css('background-color') 
					};
					console.log("STICKY OBJECT SAVE XXX");
					console.log(objectJson);
					
					var url = App.baseUrl+"CommHub/update_object_property";
					var newData = {"file_id": App.fileId,
									"page_id":App.pageId,
									"object_json":JSON.stringify(objectJson)
									};
							
					var req = $.ajax({
						type: 'POST',
						url: url,
						data:newData
					});
					req.done(function(msg){
						msg = JSON.parse(msg);
						App.currentStackIndex++;
						App.actionStack[App.currentStackIndex] = msg.history;
						var contentObject=$("#text_content_"+ emit.attr('id'));
						var data = {
							content_object:contentObject.html(),
							bg_color:emit.css('background-color'),
							height:height,
							width:width,
						};
						 
						 $("#"+id).width(width);
						 $("#"+id).height(height);
						App.socket.emit('cm_object_send', {id : id, kind: "sticky-text", cmd : "content-update" , object : data});
					});
					App.stickyText.menu.hideMenu();
				}
					
			});
			
			$("#sticky-text-cancel").click(function(){
				var r=confirm(App.localize("%cancel_confirmation"));
				if (r==true)
				{
					
					App.openedTool.removeClass('active');
					App.openedTool =false;
					App.stickyText.menu.cancel();
				}
				else
				{
					console.log("You pressed Cancel!");
				}
			});
		},
		
		createObject : function(point){
			if(App.stickyText.menu.creationMode){
				console.log("create sticky text object...");
				if(App.stickyText.menu.isShow){
					App.stickyText.menu.show();
					var id = +new Date + "";
					$("#" + App.rootCanvas)
						.append($('<div '+
								'style="background-color:#ffcc00;' + 
								'border:5px solid red;' + 
								'overflow:hidden;' +
								'position:absolute;' +
								'z-index:99;' +
								'"></div>')
						.attr("id",id)
						.addClass("nic-sticky-edit-element active-sticky").draggable({ containment: 'parent' })
					);
					$("#" + id).css({
						top:Math.floor(point.y-($("#" + id).height()/2)),
						left:Math.floor(point.x-($("#" + id).width()/2))
					});
					$("#nic-sticky-edit-panel-container" ).css({ 
						top: $("#" + id).position().top+$('#rootContainer').offset().top+$("#" + id).height()+32, 
						left: $("#" + id).position().left+$('#rootContainer').offset().left
					});
					App.stickyText.menu.id = id;
					App.stickyText.menu.panel.addInstance(id);
					console.log(App.stickyText.menu.panel);
					// $('#' + id).on('click',function(){
					$('#' + id).click(function() {
						$('#nic-edit-panel-container').draggable();
						$(this).draggable( 'disable' );
						$(this).trigger('dblclick');
						$(this).trigger('dblclick');
					});
					for(var i=0;i<$.jPicker.List.length;i++)
					{
						if($.jPicker.List[i].id == "stickyJPicker")
						{
							$.jPicker.List[i].color.active.val('hex', "ffcc00", this);
							break;
						}
					}
				}
			}
		}
	}
});